package com.sb.money.services;

import com.sb.money.dao.AbstractDao;
import com.sb.money.dao.SubcategoryDao;
import com.sb.money.dao.impl.AbstractDaoImpl;
import com.sb.money.dao.impl.SubcategoryDaoImpl;
import com.sb.money.services.impl.AbstractServiceImpl;
import com.sb.money.services.impl.SubcategoryServiceImpl;

import org.apache.tapestry5.ComponentParameterConstants;
import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.HibernateTransactionAdvisor;
import org.apache.tapestry5.ioc.MappedConfiguration;
import org.apache.tapestry5.ioc.MethodAdviceReceiver;
import org.apache.tapestry5.ioc.ServiceBinder;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.annotations.Match;
import org.apache.tapestry5.ioc.annotations.Symbol;

/**
 * This module is automatically included as part of the Tapestry IoC Registry,
 * it's a good place to configure and extend Tapestry, or to place your own
 * service definitions. See
 * http://tapestry.apache.org/5.3.5/tapestry-ioc/module.html
 */
public final class QaModule {
    /**
     * Inject production mode constant.
     */
    @Inject
    @Symbol(SymbolConstants.PRODUCTION_MODE)
    @Property(write = false)
    private static boolean productionMode;

    /**
     * utility constructor.
     */
    private QaModule() {
    }

    /**
     * Add services, daos to inject them by Tapestry.
     * 
     * @param binder
     *            to update
     */
    public static void bind(final ServiceBinder binder) {
       //dao
        binder.bind(AbstractDao.class, AbstractDaoImpl.class);
        binder.bind(SubcategoryDao.class, SubcategoryDaoImpl.class);
        // service
        binder.bind(SubcategoryService.class, SubcategoryServiceImpl.class);
        binder.bind(AbstractService.class, AbstractServiceImpl.class);


        // This next line addresses an issue affecting GlassFish and JBoss - see
        // http://blog.progs.be/?p=52
        javassist.runtime.Desc.useContextClassLoader = true;
    }

    /**
     * Add configuration for the application.
     * 
     * @param configuration
     *            to update
     */
    public static void contributeApplicationDefaults(
            final MappedConfiguration<String, String> configuration) {
        configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en");
        configuration.add(SymbolConstants.FILE_CHECK_INTERVAL, "10 m");
        configuration.add(ComponentParameterConstants.GRID_ROWS_PER_PAGE, "15");
        configuration.add(SymbolConstants.MINIFICATION_ENABLED, "false");
        configuration.add(SymbolConstants.APPLICATION_CATALOG,"app.properties");
    }

    /**
     * HibernateTransactionAdvisor to add transaction advice. This advice method
     * is configured to match against any service whose id ends with "DAO", such
     * as "PersonDAO". The advisor scans the service interface and identifies
     * any methods with the @CommitAfter annotation.
     * 
     * @param advisor
     *            to use
     * @param receiver
     *            to use
     */
    @Match("*Dao*")
    public static void adviseTransactions(
            final HibernateTransactionAdvisor advisor,
            final MethodAdviceReceiver receiver) {
        advisor.addTransactionCommitAdvice(receiver);
    }
    
    
}